package com.example.library_common.utils.decoration;

import android.graphics.Rect;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.library_common.utils.PixUtils;

public class GridSpacingDecoration extends RecyclerView.ItemDecoration {

    private int spanCount = 1;

    private int spaceSize = PixUtils.dp2px(10);

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

//        super.getItemOffsets(outRect, view, parent, state);

        RecyclerView.Adapter adapter = parent.getAdapter();



        //这里是关键，需要根据你有几列来判断
        // item position
        int position = parent.getChildAdapterPosition(view);

        int spanSize = 1;

        int headerCount=0;

        int dataPos=0;

        if (adapter != null) {

            int prePos = position - 1 >= 0 ? position - 1 : position;

            if (adapter instanceof BaseQuickAdapter){
                headerCount= ((BaseQuickAdapter) adapter).getHeaderLayoutCount();
                dataPos=position>=headerCount?position-headerCount:position;
            }
        }

        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            GridLayoutManager managers = (GridLayoutManager) manager;
            spanCount = managers.getSpanCount();
            GridLayoutManager.SpanSizeLookup lookup = managers.getSpanSizeLookup();
            if (lookup != null) {
                spanSize = lookup.getSpanSize(position);

            }

        }

        if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager managers = (StaggeredGridLayoutManager) manager;
            spanCount = managers.getSpanCount();
        }

        // item column
        int column = dataPos % spanCount;


        if (spanSize == spanCount) {

            if (position == 0) {
                outRect.top = spaceSize;
            } else {
                outRect.top = spaceSize / 2;
            }
            outRect.bottom = spaceSize / 2;
            outRect.left = 0;
            outRect.right = 0;
        } else if (spanCount % 2 != 0) {

            outRect.left = spaceSize - column * spaceSize / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spaceSize / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spaceSize;
            }
            outRect.bottom = spaceSize; // item bottom
        } else if (isOnLeft(column)) {
            outRect.left = spaceSize;
            outRect.right = spaceSize / 2;
            outRect.top = spaceSize / 2;
            outRect.bottom = spaceSize / 2;
        } else if (isOnRight(column)) {

            outRect.left = spaceSize / 2;
            outRect.right = spaceSize;

            outRect.top = spaceSize / 2;
            outRect.bottom = spaceSize / 2;
        } else {
            outRect.left = spaceSize / 2;
            outRect.right = spaceSize / 2;
            outRect.bottom = spaceSize / 2;
            outRect.top = spaceSize / 2;
        }


    }

    private boolean isOnRight(int column) {
//        if (position < spanCount) {
//            return Math.abs(position - spanCount) == 1;
//        }
        return column == 1;
    }

    private boolean isOnLeft(int column) {
//        if (position < spanCount) {
//            return Math.abs(position - spanCount) == spanCount;
//        }
        return column == 0;
    }
}
